Test Setup Failed
Push — master ( 51607c...6b8ca8 )
by Paul
03:43
created

GLSR.shortcode.trigger   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
rs 9.2
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
GLSR.shortcode.close = function( el )
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
2
{
3
	var button = x(( el = el || '.glsr-mce-button' ));
0 ignored issues
show
Unused Code introduced by
The assignment to variable el seems to be never used. Consider removing it.
Loading history...
4
	if( button.length ) {
5
		button.removeClass( 'active' ).parent().find( '.glsr-mce-menu' ).hide();
6
	}
7
};
8
9
GLSR.shortcode.open = function( el )
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
{
11
	x( el ).addClass( 'active' ).parent().find( '.glsr-mce-menu' ).show();
12
};
13
14
GLSR.shortcode.toggle = function( ev )
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
15
{
16
	ev.preventDefault();
17
	if( x( this ).hasClass( 'active' ) ) {
18
		GLSR.shortcode.close( this );
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
19
	}
20
	else {
21
		GLSR.shortcode.open( this );
22
	}
23
};
24
25
GLSR.shortcode.trigger = function( ev )
26
{
27
	ev.preventDefault();
28
	// GLSR.shortcode.current is used by scForm to trigger the correct popup
29
	GLSR.shortcode.current = x( this ).attr( 'data-shortcode' );
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
30
	if( !GLSR.shortcode.current )return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
31
	if( !tinymce.get( window.wpActiveEditor ) ) {
0 ignored issues
show
Bug introduced by
The variable tinymce seems to be never declared. If this is a global, consider adding a /** global: tinymce */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
32
		// Quicktags Editor
33
		if( !x( '#scTemp' ).length ) {
34
			x( 'body' ).append( '<textarea id="scTemp" style="display: none;" />' );
35
			tinymce.init({
36
				mode     : 'exact',
37
				elements : 'scTemp',
38
				plugins  : ['glsr_shortcode', 'wplink']
39
			});
40
		}
41
		setTimeout( function() {
42
			tinymce.execCommand( 'GLSR_Shortcode' );
0 ignored issues
show
Bug introduced by
The variable tinymce seems to be never declared. If this is a global, consider adding a /** global: tinymce */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
43
		}, 200 );
44
	}
45
	else {
46
		// TinyMCE Editor
47
		tinymce.execCommand( 'GLSR_Shortcode' );
48
	}
49
	setTimeout( function() {
50
		GLSR.shortcode.close();
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
51
	}, 100 );
52
};
53
54
GLSR.shortcode.create = function( editor_id )
55
{
56
	var editor = tinymce.get( editor_id );
0 ignored issues
show
Bug introduced by
The variable tinymce seems to be never declared. If this is a global, consider adding a /** global: tinymce */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
57
	if( !editor )return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
58
	var data = {
59
		action: site_reviews.action,
0 ignored issues
show
Bug introduced by
The variable site_reviews seems to be never declared. If this is a global, consider adding a /** global: site_reviews */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
60
		request: {
61
			action: 'mce-shortcode',
62
			nonce: x( '#_glsr_nonce' ).val(),
63
			shortcode: GLSR.shortcode.current,
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
64
		},
65
	};
66
	x.post( site_reviews.ajaxurl, data, function( response )
0 ignored issues
show
Bug introduced by
The variable x seems to be never declared. If this is a global, consider adding a /** global: x */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
67
	{
68
		if( !response.body )return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
69
		if( response.body.length === 0 ) {
70
			window.send_to_editor( '[' + response.shortcode + ']' );
71
			GLSR.shortcode.destroy();
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
72
			return;
73
		}
74
		var buttons = [{
75
			text    : response.ok,
76
			classes : 'btn glsr-btn primary',
77
			onclick : function() {
78
				var field, required, valid, win;
79
				// Get the top most window object
80
				win = editor.windowManager.getWindows()[0];
81
				// Get the shortcode required attributes
82
				required = site_reviews.shortcodes[ GLSR.shortcode.current ];
0 ignored issues
show
Bug introduced by
The variable site_reviews seems to be never declared. If this is a global, consider adding a /** global: site_reviews */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
83
				valid = true;
84
				// Do some validation voodoo
85
				for( var id in required ) {
86
					if( !required.hasOwnProperty( id ) )continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
87
					field = win.find( '#' + id )[0];
88
					if( typeof field !== 'undefined' && field.state.data.value === '' ) {
89
						valid = false;
90
						alert( required[ id ] );
91
						break;
92
					}
93
				}
94
				if( valid ) {
95
					win.submit();
96
				}
97
			}
98
		},{
99
			text    : response.close,
100
			onclick : 'close'
101
		}];
102
		var popup = {
103
			title   : response.title,
104
			body    : response.body,
105
			classes: 'glsr-mce-popup',
106
			minWidth: 320,
107
			buttons : buttons,
108
			onsubmit: function( e ) {
109
				var attributes = '';
110
				var data = GLSR.shortcode.normalize( e.data );
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
111
				for( var key in data ) {
112
					if( data.hasOwnProperty( key ) && data[ key ] !== '' ) {
113
						attributes += ' ' + key + '="' + data[ key ] + '"';
114
					}
115
				}
116
				// Insert shortcode into the WP_Editor
117
				window.send_to_editor( '[' + response.shortcode + attributes + ']' );
118
			},
119
			onclose: function() {
120
				GLSR.shortcode.destroy();
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
121
			}
122
		};
123
		// Change the buttons if server-side validation failed
124
		if( response.ok.constructor === Array ) {
125
			popup.buttons[0].text    = response.ok[0];
126
			popup.buttons[0].onclick = 'close';
127
			delete popup.buttons[1];
128
		}
129
		editor.windowManager.open( popup );
130
	});
131
};
132
133
GLSR.shortcode.normalize = function( data )
134
{
135
	var shortcodeHiddenFields = {
136
		'site_reviews' : ['author','date','excerpt','rating','response','title'],
137
		'site_reviews_form': ['email','name','terms','title'],
138
		'site_reviews_summary': ['bars','if_empty','rating','stars','summary'],
139
	};
140
	var hide = [];
141
	for( var key in data ) {
142
		if( !data.hasOwnProperty( key ) )continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
143
		if( shortcodeHiddenFields.hasOwnProperty( GLSR.shortcode.current ) ) {
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
144
			var value = '';
145
			if( key.lastIndexOf( 'hide_', 0 ) === 0 ) {
146
				value = key.substring(5);
147
			}
148
			if( shortcodeHiddenFields[ GLSR.shortcode.current ].indexOf( value ) > -1 ) {
149
				if( data[ key ] ) {
150
					hide.push( value );
151
				}
152
				delete data[ key ];
153
			}
154
		}
155
		if( key === 'count' && !x.isNumeric( data[ key ] ) ) {
0 ignored issues
show
Bug introduced by
The variable x seems to be never declared. If this is a global, consider adding a /** global: x */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
156
			data[ key ] = '';
157
		}
158
		if( key === 'id' ) {
159
			data[ key ] = (+new Date()).toString(36);
160
		}
161
	}
162
	data.hide = hide.join( ',' );
163
	return data;
164
};
165
166
GLSR.shortcode.destroy = function()
167
{
168
	var tmp = x( '#scTemp' );
169
	if( tmp.length ) {
170
		tinymce.get( 'scTemp' ).remove();
0 ignored issues
show
Bug introduced by
The variable tinymce seems to be never declared. If this is a global, consider adding a /** global: tinymce */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
171
		tmp.remove();
172
	}
173
};
174